/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_NET_SOCKET_H_
#define SHARED_NET_SOCKET_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <cstddef>
#include <string>
#include <memory>
#include <event2/util.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include "Shared/Global_Macros.h"


namespace Shared {
namespace Net {
    class SocketEventHandler;
    class TcpServer;
	class TcpThread;

    class Socket
    {
    public:
        Socket(void);
        ~Socket(void);

        bool open(struct event_base *evBase, evutil_socket_t fd, 
            struct sockaddr *sa = NULL, int sockLen = 0);
        void close(void);

        int id(void) const { return _id; }
        const std::string& ip(void) const { return _ip; }
        int port(void) const { return _port; }
        const std::string& errorString(void) const { return _errString; }
        int errorCode(void) const { return _errCode; }

        void send(const std::uint8_t *data, std::size_t size);

        std::size_t receiveSize(void);
        int receive(std::uint8_t *data, std::size_t size);
        int receivePeek(std::uint8_t *data, std::size_t size);
        std::size_t receiveSkip(std::size_t size);

        void enableRead(bool enable);
        void enableWrite(bool enable);
        void shutdownRead(void);
        void shutdownWrite(void);

    private:
        friend class TcpServer;

        void setId(int id) { _id = id; }
        void setOwner(TcpServer *owner) { _owner = owner; }
		void setThread(TcpThread *thread) { _thread = thread; }
        void setEventHandler(SocketEventHandler *handler) { _evHandler.reset(handler); }
        void unintialize(void);

        void onError(void);

        static void readCallback(struct bufferevent *, void *);
        static void writeCallback(struct bufferevent *, void *);
        static void eventCallback(struct bufferevent *, short, void *);

        int _id;
        evutil_socket_t _fd;
        struct bufferevent *_bev;
        std::auto_ptr<SocketEventHandler> _evHandler;
        std::string _ip;
        int _port;
        std::string _errString;
        int _errCode;
        volatile bool _fatalError;
        TcpServer *_owner;
		TcpThread *_thread;
        DISALLOW_COPY_AND_ASSIGN(Socket);
    };

}}

#endif // SHARED_NET_SOCKET_H_
